package com.lt.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lt.reggie.dto.DishDto;
import com.lt.reggie.entity.Dish;
import com.lt.reggie.entity.DishFlavor;
import com.lt.reggie.mapper.DishMapper;
import com.lt.reggie.service.DishFlavorService;
import com.lt.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存口味
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);
        //保存对应菜品的口味到口味表
        Long dishId = dishDto.getId();//菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();//菜品口味
        //对集合进行加工处理，加上dishId后再加工成集合返回
        flavors = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishId);
            return flavor;
        }).collect(Collectors.toList());
        //对集合进行遍历加入dishId
//        for (DishFlavor flavor : flavors) {
//            flavor.setDishId(dishId);
//        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品和口味
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据菜品id在dish表查菜品基本信息
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        //将dish对象中的属性copy到dishDto中
        BeanUtils.copyProperties(dish,dishDto);

        //构建条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);

         //将查询得到的口味数据加入到dishDto中
        dishDto.setFlavors(dishFlavors);

        return dishDto;
    }

    /**
     *  修改菜品
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //跟新dish表基本信息
        this.updateById(dishDto);
        //跟新前先清理数据库中对应的口味数据
        LambdaQueryWrapper<DishFlavor> deleteQueryWrapper = new LambdaQueryWrapper<>();
        deleteQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(deleteQueryWrapper);

        //跟新dishFlavor表信息
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除单个或批量菜品和口味
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavor(List<String> ids) {
        this.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> deleteQueryWrapper = new LambdaQueryWrapper<>();
        deleteQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(deleteQueryWrapper);

    }

    /**
     * 单个或批量停售或起售菜品
     * @param status
     * @param ids
     */
    @Override
    public void updateOrListWithFlavor(int status, List<String> ids) {
        Dish dish = new Dish();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        dish.setStatus(status);
        this.update(dish,queryWrapper);
    }
}
